/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AddLayerForm.java
 *
 * Created on May 11, 2011, 1:53:14 PM
 */
package com.iceberg.adgt.ui.gen;

import com.iceberg.adgt.common.DomainException;
import com.iceberg.adgt.domain.DomainFacade;
import com.iceberg.adgt.domain.DomainFacadeImpl;
import com.iceberg.adgt.domain.UnitTypeDTO;
import com.iceberg.adgt.ui.architecture.ArchitectureController;

/**
 * @author David
 */
class AddUnitForm extends javax.swing.JPanel {

	/**
	 * Creates new form AddLayerForm
	 */
	public AddUnitForm() {

		initComponents();
		init();
	}

	/**
	 * This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		addUnitLabel = new javax.swing.JLabel();
		addUnitField = new javax.swing.JTextField();
		addUnitButton = new javax.swing.JButton();
		addUnitErrorMsg = new javax.swing.JLabel();
		typeBox = new javax.swing.JComboBox();

		addUnitLabel.setText("Name:");

		addUnitField.setText("Unit Name");

		addUnitButton.setText("Add unit");
		addUnitButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				addUnitButtonActionPerformed(evt);
			}
		});

		addUnitErrorMsg.setForeground(java.awt.Color.red);

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
		this.setLayout(layout);
		layout.setHorizontalGroup(
				layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(layout.createSequentialGroup()
								.addComponent(addUnitLabel)
								.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(addUnitField, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(typeBox, 0, 91, Short.MAX_VALUE)
								.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(addUnitButton)
								.addGap(20, 20, 20))
						.addGroup(layout.createSequentialGroup()
								.addComponent(addUnitErrorMsg)
								.addContainerGap())
		);
		layout.setVerticalGroup(
				layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(layout.createSequentialGroup()
								.addComponent(addUnitErrorMsg, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
										.addComponent(addUnitLabel)
										.addComponent(addUnitField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
										.addComponent(addUnitButton)
										.addComponent(typeBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
		);
	}// </editor-fold>//GEN-END:initComponents

	private void addUnitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addUnitButtonActionPerformed

		if (this.controller.getSelectedLayer() != null) {
			try {
				this.controller.addUnitToLayer(this.addUnitField.getText(), typeBox.getSelectedItem().toString());
				addUnitErrorMsg.setText("");
			} catch (DomainException de) {
				addUnitErrorMsg.setText(de.getMessage());
			}

		} else {
			addUnitErrorMsg.setText("The unit cannot be added, because there is no layer selected.");
		}
	}//GEN-LAST:event_addUnitButtonActionPerformed

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JButton addUnitButton;
	private javax.swing.JLabel addUnitErrorMsg;
	private javax.swing.JTextField addUnitField;
	private javax.swing.JLabel addUnitLabel;
	private javax.swing.JComboBox typeBox;
	// End of variables declaration//GEN-END:variables
	private ArchitectureController controller;

	public final void setController(ArchitectureController controller) {
		this.controller = controller;
	}

	private void init() {
		DomainFacade facade = DomainFacadeImpl.getInstance();
		for (UnitTypeDTO type : facade.getUnitTypes()) {
			this.typeBox.addItem(type.getName());
		}
	}
}